package gui.league;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JComboBox;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SpinnerDateModel;
import java.util.Date;
import java.util.Calendar;
import javax.swing.JButton;

import application.ArenaManager;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class League extends JPanel {
	private JTextField tournamentNametxtField;
	private JComboBox comboBoxSyle;
	private JSpinner endDateSpinner;
	private JSpinner numPlayersSpinner;
	
	/**
	 * Create the panel.
	 */
	
	
	
	
	public League() {
		setLayout(new BorderLayout(0, 0));
		
		JLabel lblNewTournament = new JLabel("New Tournament");
		add(lblNewTournament, BorderLayout.NORTH);
		
		JPanel newTournamentPanel = new JPanel();
		newTournamentPanel.setBorder(new TitledBorder(null, "Tournament Data", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		add(newTournamentPanel, BorderLayout.WEST);
		
		JPanel pNamePanel = new JPanel();
		pNamePanel.setBorder(new TitledBorder(null, "Tournament Name", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JPanel tStylePanel = new JPanel();
		tStylePanel.setBorder(new TitledBorder(null, "Tournament Style", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		tStylePanel.setLayout(new BorderLayout(0, 0));
		
		JPanel numPlayersPanel = new JPanel();
		numPlayersPanel.setBorder(new TitledBorder(null, "Num Players", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		numPlayersPanel.setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "End Date", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setLayout(new BorderLayout(0, 0));
		
		JButton btnOK = new JButton("OK");
		btnOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				saveTournament();
			}

			
		});
		GroupLayout gl_newTournamentPanel = new GroupLayout(newTournamentPanel);
		gl_newTournamentPanel.setHorizontalGroup(
			gl_newTournamentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_newTournamentPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_newTournamentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_newTournamentPanel.createParallelGroup(Alignment.LEADING)
							.addComponent(tStylePanel, GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
							.addComponent(pNamePanel, GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
							.addComponent(numPlayersPanel, GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
							.addComponent(panel, GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE))
						.addGroup(Alignment.TRAILING, gl_newTournamentPanel.createSequentialGroup()
							.addComponent(btnOK)
							.addGap(43))))
		);
		gl_newTournamentPanel.setVerticalGroup(
			gl_newTournamentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_newTournamentPanel.createSequentialGroup()
					.addContainerGap()
					.addComponent(pNamePanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(tStylePanel, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(numPlayersPanel, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnOK)
					.addContainerGap(17, Short.MAX_VALUE))
		);
		
		numPlayersSpinner = new JSpinner();
		numPlayersSpinner.setModel(new SpinnerNumberModel(1, null, 64, 1));
		numPlayersPanel.add(numPlayersSpinner, BorderLayout.CENTER);
		
		endDateSpinner = new JSpinner();
		endDateSpinner.setModel(new SpinnerDateModel(new Date(1326495600000L), null, null, Calendar.DAY_OF_MONTH));
		panel.add(endDateSpinner, BorderLayout.CENTER);
		
		//comboBoxSyle = new JComboBox();
		initComboBox();
		tStylePanel.add(comboBoxSyle, BorderLayout.CENTER);
		pNamePanel.setLayout(new BorderLayout(0, 0));
		
		tournamentNametxtField = new JTextField();
		pNamePanel.add(tournamentNametxtField, BorderLayout.CENTER);
		tournamentNametxtField.setColumns(10);
		newTournamentPanel.setLayout(gl_newTournamentPanel);
				
		add(tournamentList(), BorderLayout.CENTER);

	}
	
	private JList tournamentList() {
		
		JList tournamentList = new JList(ArenaManager.getInstance().getTournaments(ArenaManager.getInstance().getUser()).toArray());
		//JList tournamentList = new JList();
		tournamentList.setBorder(new TitledBorder(null, "Tournaments", TitledBorder.LEADING, TitledBorder.TOP, null, null));		
		
		return tournamentList;
	
	}
	private String style;
	private void initComboBox() {
		String[] styles = {"FreePlay", "DeathMatch"};
		comboBoxSyle = new JComboBox(styles);

	}
	
	private void saveTournament() {
		String tournamentName = tournamentNametxtField.getText();
		int numPlayers = (Integer)numPlayersSpinner.getValue();
		Date duration = (Date)endDateSpinner.getValue();
		String style = (String)comboBoxSyle.getSelectedItem();
		ArenaManager.getInstance().addTournament(tournamentName,numPlayers, style, duration);
		
	}
	
	
}
